package com.hileto.inf.utils;

/** 
 * <p>Title:request转化为javaBean工具类</p> 
 * <p>Description:request转化为javaBean工具类</p> 
 */  
  
import java.lang.reflect.Field;  
import java.lang.reflect.Method;  
import java.sql.Timestamp;  
import java.text.SimpleDateFormat;  
import java.util.Enumeration;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.List;  
import java.util.Map;  
  
import javax.servlet.http.HttpServletRequest;  
  
public class RequestBeanUtils {  
  
    final static String PREFFIX = "set";  
      
    /** 
     *  
     * 把request中的值赋给Object对象,也就是把request中的parameter和object属性对应的时候 
     * 把object中的属性赋值,使用标准的bean 
     * @param  request 
     * @param  target 目标bean 
     * @return 封装之后的bean 
     * @throws Exception 
     */  
    public static Object getValuesByRequest(HttpServletRequest request,Object target) throws Exception {  
        Enumeration<String> keys = request.getParameterNames();  
        Map<String, String> valueMap = getValuesMap(request, keys);  
        getObject(valueMap, target, null,null);  
        return target;  
    }  
  
  
    /** 
     *  
     * @see    把request中的值赋给Object对象,也就是把request中的parameter和object属性对应的时候 
     *         把object中的属性赋值,使用标准的bean 
     * @param  request 
     * @param  target 目标bean 
     * @param  unSetProperties 不复制的对象 
     * @return 封装之后的bean 
     * @throws Exception 
     */  
    public static Object getValuesByRequest(HttpServletRequest request,  
            Object object,  String[] unSetProperties)throws Exception {  
        Enumeration<String> keys = request.getParameterNames();  
        Map<String, String> valueMap = getValuesMap(request, keys);  
        getObject(valueMap, object, unSetProperties,null);  
        return object;  
    }  
  
    /** 
     *  
     * @see    把request中的值赋给Object对象,也就是把request中的parameter和object属性对应的时候 
     *         把object中的属性赋值,使用标准的bean 
     * @param  request 
     * @param  target 目标bean 
     * @param  unSetProperties 不复制的对象 
     * @param  proertiesMap 不一致属性匹配 
     * @return 封装之后的bean 
     * @throws Exception 
     */  
    public static Object getValuesByRequest(HttpServletRequest request,  
            Object object,  String[] unSetProperties,Map<String,String> proertiesMap)  
            throws Exception {  
        Enumeration<String> keys = request.getParameterNames();  
        Map<String, String> valueMap = getValuesMap(request, keys);  
        getObject(valueMap, object, unSetProperties,proertiesMap);  
        return object;  
    }  
      
    /** 
     * 获取request参数及值map(键值对) 
     * @param request 
     * @param keys 
     * @return 
     * @throws Exception 
     */  
    protected static Map<String, String> getValuesMap(  
            HttpServletRequest request, Enumeration<String> keys) throws Exception {  
        if (keys == null)  
            return null;  
        Map<String, String> value = new HashMap<String, String>();  
        while (keys.hasMoreElements()) {  
            String element = keys.nextElement();  
            value.put(element, request.getParameter(element));  
        }  
        return value;  
    }  
  
    /** 
     * 转化javaBean 
     * @param valueMap request获取的键值 
     * @param object javaBean对象 
     * @param unSetProperties 不复制的对象 
     * @param proertiesMap 不一致属性匹配 
     * @return 
     * @throws Exception 
     */  
    protected static Object getObject(Map<String, String> valueMap,  
            Object object, String[] unSetProperties,Map<String,String> proertiesMap) throws Exception {  
        List<String> unSet = null;  
        if (unSetProperties != null)  
            unSet=java.util.Arrays.asList(unSetProperties);  
        if (valueMap == null)  
            return null;  
        for (Iterator<String> iter=valueMap.keySet().iterator();iter.hasNext();) {  
            String key = iter.next();  
            if (unSet != null && unSet.contains(key)) {  
                continue;  
            }  
            String methodValue = valueMap.get(key);  
            if(proertiesMap!=null && proertiesMap.containsKey(key)){  
                key=proertiesMap.get(key);  
            }  
            if (methodValue != null)  
                setValue(object, key, methodValue);  
        }  
        return object;  
    }  
  
    /** 
     * 给javaBean中的属性字段赋值 
     * @param obj  javaBean对象 
     * @param name bean属性 
     * @param value 值 
     * @throws Exception 
     */  
    protected static void setValue(Object obj, String name, String value)  
            throws Exception {  
        Field field = null;  
        try {  
            field = obj.getClass().getDeclaredField(name);  
        } catch (Exception e) {  
            return;  
        }  
        if (field == null) {  
            return;  
        }  
        Method method = obj.getClass().getDeclaredMethod(  
                getSetMethodName(name), field.getType());  
        String type = field.getGenericType().toString();  
        if(type.equals("int") || type.equals("class java.lang.Integer")){  
            if(value!=null && !value.equals("")){  
                method.invoke(obj, Integer.parseInt(value));  
            }  
        }else if(type.equals("double") || type.equals("class java.lang.Double")){  
            if(value!=null && !value.equals("")){  
                method.invoke(obj, Double.valueOf(value));  
            }  
        }else if(type.equals("float") || type.equals("class java.lang.Float")){  
            if(value!=null && !value.equals("")){  
                method.invoke(obj, Float.valueOf(value));  
            }  
        }else if(type.equals("char") || type.equals("class java.lang.Character")){  
            if(value!=null && !value.equals("")){  
                method.invoke(obj, Character.valueOf(value.charAt(0)));  
            }  
        }else if(type.equals("class java.util.Date")){  
            if(value!=null && !value.equals("")){  
                String format = value.length()==10?"yyyy-MM-dd":"yyyy-MM-dd HH:mm:ss";  
                SimpleDateFormat sdf = new SimpleDateFormat(format);  
                method.invoke(obj, sdf.parse(value));  
            }                  
        }else if(type.equals("class java.sql.Timestamp")){  
            if(value!=null && !value.equals("")){  
                String format = value.length()==10?"yyyy-MM-dd":"yyyy-MM-dd HH:mm:ss";  
                SimpleDateFormat sdf = new SimpleDateFormat(format);  
                method.invoke(obj, new Timestamp(sdf.parse(value).getTime()));  
            }  
        }else{  
            method.invoke(obj, value);  
        }  
    }  
  
    /** 
     * bean属性转化为set方法 
     * @param methodName 
     * @return 
     */  
    protected static String getSetMethodName(String methodName) {  
        String first = methodName.substring(0, 1);  
        String suffex = methodName.substring(1);  
        return (PREFFIX + first.toUpperCase() + suffex).toString();  
    }  
}  